package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingsApiImpl implements SettingsApi{

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 通过用户id查询设置信息
     * @param userId
     * @return
     */
    @Override
    public Settings findByUserId(Long userId) {
        return settingsMapper.selectOne(
                Wrappers.<Settings>lambdaQuery()
                        .eq(Settings::getUserId, userId)
        );
    }

    /**
     * 保存设置
     * @param settings
     */
    @Override
    public void save(Settings settings) {
        Settings settingsInDB = settingsMapper.selectOne(Wrappers.<Settings>lambdaQuery()
                .eq(Settings::getUserId, settings.getUserId()));
        if (null != settingsInDB) {
            settings.setId(settingsInDB.getId());
            settingsMapper.updateById(settings);
        }else {
            settingsMapper.insert(settings);
        }
    }
}
